<?php
/*
 * Created on 07-06-2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class NormalReservationsController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'NormalReservations';

/**
 * Default helper
 *
 * @var array
 */
	public $helpers = array();

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Court', 'User', 'Reservation');
	
	public function isAuthorized($user) {
		if (in_array($user['role'], array('admin', 'superAdmin'))) {
			return true;
		}
		if(in_array($this->action, array('delete'))) {
			$reservation = $this->Reservation->find('first', array('conditions'=> array('Reservation.id'=>$this->request->params['pass'][0])));
			if($user['id'] != $reservation['Reservation']['user_id']){
				return false;
			}
			return true;
		}
		if(in_array($this->action, array('add', 'jsSetAvailableHours'))) {
			return true;
		}
		return false;
	}

	public function add() {
		$this->set('title_for_layout', 'Rezerwacje - dodaj rezerwację');
		$user = $this->Auth->user();
		if($this->request->is('post')) {
			$data = $this->request->data;
			$data['NormalReservation']['user_id'] = $user['id'];
			$data['Reservation'] = $data['NormalReservation'];
			unset($data['NormalReservation']);
			if($this->Reservation->save($data)){
				$this->Session->setFlash('Rezerwacja została dodana.', 'flash_success');
			} else {
				$this->Session->setFlash('Nie udało się dodać rezerwacji.', 'flash_failure');
			}
		}

		$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));
	}
	
	public function delete($id = null) {
		$user = $this->Auth->user();
		$this->Reservation->id = $id;
		if (!$this->Reservation->exists()) {
			$this->Session->setFlash('Nie ma takiej rezerwacji.', 'flash_failure');
			$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));
		}
		if ($this->Reservation->delete()) {
			$this->Session->setFlash('Rezerwacja usunięta.', 'flash_success');
			$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));
		}
		$this->Session->setFlash('Nie udało się usunąć rezerwacji.', 'flash_failure');
		$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));	
	}
}
?>
